package com.wangl.generator.wlmybatisgenerator.utils;

import com.wangl.generator.wlmybatisgenerator.data.DataStore;

import javax.xml.crypto.Data;
import java.sql.*;
import java.util.LinkedList;
import java.util.List;

/**
 * @ClassName JdbcUtil
 * @Description TODO
 * @Author wangl
 * @Date 2021/11/26 16:59
 */
public class JdbcUtil {

    static final String DB_URL = "jdbc:mysql://:3306/?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&useSSL=false";
    static final String USER = "ro_tester";
    static final String PASS = "b270c7dd7e7bd94d4e7f4caa41452902";

    public static List<String> allDatabases(){
        String url = "jdbc:mysql://" + DataStore.host + ":" + DataStore.port + "/?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&useSSL=false";
        List<String> databases = new LinkedList<>();
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        try(Connection conn = DriverManager.getConnection(url, DataStore.username, DataStore.password);
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery("show databases");) {
            // Extract data from result set
            while (rs.next()) {
                databases.add(rs.getString(1));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return databases;
    }

    public static List<String> allTables(String database){
        String url = "jdbc:mysql://" + DataStore.host + ":" + DataStore.port + "/?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&useSSL=false";
        List<String> tables = new LinkedList<>();
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        try(Connection conn = DriverManager.getConnection(url, DataStore.username, DataStore.password);
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery("SELECT table_name FROM information_schema. TABLES WHERE table_schema = '" + database + "';")) {
            // Extract data from result set
            while (rs.next()) {
                tables.add(rs.getString(1));
            }
            DataStore.tables = tables;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return tables;
    }

    public static boolean testConnection(){
        String url = "jdbc:mysql://" + DataStore.host + ":" + DataStore.port + "/?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&useSSL=false";
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        try(Connection conn = DriverManager.getConnection(url, DataStore.username, DataStore.password);) {
            return conn != null;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }
}
